package com.appcenter.application.service;

import com.appcenter.application.dto.orgs.OrgDto;
import com.appcenter.application.dto.roles.RoleDto;
import com.appcenter.application.dto.users.UserDto;
import com.appcenter.application.expand.ObjectFinal;
import com.appcenter.application.input.org.GetOrgsInput;
import com.appcenter.application.input.userpicker.LoadUserPickerListPageInput;
import com.appcenter.application.input.userpicker.SearchUserPickerListInput;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.RolesMapper;
import com.appcenter.application.mapper.UsersMapper;
import com.appcenter.application.model.Apps;
import com.appcenter.application.model.Orgs;
import com.appcenter.application.model.Roles;
import com.appcenter.application.model.Users;
import com.appcenter.application.output.userpicker.LoadUserPickerListPageOutput;
import com.appcenter.application.output.userpicker.SearchUserPickerListOutput;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.http.util.TextUtils.isBlank;

@Service
public class UserPickerService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    AppsMapper appsMapper;
    @Autowired
    OrgUserService orgUserService;
    @Autowired
    RolesMapper rolesMapper;
    @Autowired
    OrgService orgService;
    @Autowired
    ObjectFinal objectFinal;


    /**
     *  创建人:Coke
     *  备注: 加载选人控件
     *  时间: 2019/4/26 14:06
     *  参数:
     */
    public LoadUserPickerListPageOutput LoadUserPickerListPage(LoadUserPickerListPageInput input) throws Exception {
        LoadUserPickerListPageOutput output=new LoadUserPickerListPageOutput();

        GetOrgsInput orgsInput=new GetOrgsInput();
        orgsInput.setAppCode(input.getAppCode());
        List<OrgDto> orgDtos =orgService.GetOrgs(orgsInput).getOrgs();

        // Example example = new Example(Roles.class);
        // Example.Criteria criteria = example.createCriteria();
        // criteria.andEqualTo("AppCode", input.getAppCode());
        // List<Roles> roles=rolesMapper.selectByExample(example);
        List<RoleDto> roles=usersMapper.GetRolesByAppCpde(input.getAppCode());
        output.setRoles(roles);
        output.setOrgs(orgDtos);
        output.setItems(SearchUserPickerList(input).getItems());
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 选人控件搜索
     *  时间: 2019/4/26 14:07
     *  参数:
     */
    public SearchUserPickerListOutput SearchUserPickerList(SearchUserPickerListInput input) throws Exception {

        if (isBlank(input.getSearchType()) || input.getSearchType().equals("User")) {
            return SearchUsers(input);
        }
        if (input.getSearchType().equals("Role")) {
            return SearchRoleUsers(input);
        }
        else  if (input.getSearchType().equals("Org")) {
            return SearchOrgUsers(input);
        }
        else{
            return  new SearchUserPickerListOutput();
        }
    }
    /**
     *  创建人:Coke
     *  备注: 选人控件搜索
     *  时间: 2019/4/26 14:07
     *  参数:
     */
    private SearchUserPickerListOutput SearchUsers(SearchUserPickerListInput input) {
        SearchUserPickerListOutput output=new SearchUserPickerListOutput();
        if (isBlank(input.getAppCode())){
            input.setAppCode(null);
        }
        if ( input.getIsHr()==null || input.getIsHr()) {
            input.setAppCode(null);
        }
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize());
        List<UserDto> ls=usersMapper.SearchUsers(input);
        PageInfo<UserDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<UserDto>(page.getList(),(int)page.getTotal()));

        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 选人控件搜索
     *  时间: 2019/4/26 14:07
     *  参数:
     */
    private SearchUserPickerListOutput SearchRoleUsers(SearchUserPickerListInput input) throws Exception {
        SearchUserPickerListOutput output=new SearchUserPickerListOutput();
        List<UserDto> ls=new ArrayList<>();
        if (!isBlank(input.getRoleId())){
            PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize());
            input.setVersionNo(objectFinal.getDataEffectiveVersion());
            ls=usersMapper.SearchRoleUsers(input);
            PageInfo<UserDto> page = new PageInfo<>(ls);
        }
        output.setItems(new PagedResult<UserDto>(ls,ls.size()));
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 选人控件搜索
     *  时间: 2019/4/26 14:07
     *  参数:
     */
    private SearchUserPickerListOutput SearchOrgUsers(SearchUserPickerListInput input) throws Exception {
        SearchUserPickerListOutput output=new SearchUserPickerListOutput();
        List<UserDto> ls=new ArrayList<>();
        if (!isBlank(input.getOrgId())){
            PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize());
            input.setVersionNo(objectFinal.getDataEffectiveVersion());
            ls=usersMapper.SearchOrgUsers(input);
            PageInfo<UserDto> page = new PageInfo<>(ls);
        }
        output.setItems(new PagedResult<UserDto>(ls,ls.size()));
        return output;
    }
}
